package com.fileuptool.service;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;


import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.fileuptool.utils.FileOperateUtil;

/**
 * 
 * @author dinghao
* @ClassName: FileDownService 
* @Description: TODO
* @date 2018年3月15日 下午4:40:36 
*
 */

@Service
public class FileDownService {

	public ResponseEntity<byte[]> getFile(String filename,String filetype) throws IOException {
		File file = new File("F:\\tempFile\\" + filename + "." + filetype);
		if(file.exists()) {
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			headers.setContentDispositionFormData("attachment", URLEncoder.encode(filename + "." + filetype, "UTF-8"));
			return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
		}
		return null;
	}
	
	
	public String getFile2(String filename,String filetype) {
		String filePath = "D:\\tempFile";
		File file1 = new File(filePath);
		if (!file1.exists()) {
			file1.mkdirs();
		}
		File file = new File("F:\\tempFile\\" + filename + "." + filetype);
		
		if(file.exists()) {
			return FileOperateUtil.getInstance().writeFileToService(FileOperateUtil.getInstance().getBytes(file), filePath, filename + "." + filetype);
		}
		return null;
	}
	
}
